<?php
class ContentTag{
	public $tag=array(
		'arclist'=>array('block'=>1,'level'=>4),
		'channel'=>array('block'=>1,'level'=>4),
		'pagelist'=>array('block'=>1,'level'=>4),
		'pageshow'=>array('block'=>0,'level'=>4)
	);
	
//*****************************   _arclist读取文章列表   ***********************
	// * @param  [type] $attr    [description]
	// * @param  [type] $content [description]
	//* @return [type]          [description]
	
	// public function _arclist($att,$content){
	// 	$cid=isset($attr['cid'])

	// }

	public function _arclist($attr,$content){
		$cid = isset($attr['cid'])?$attr['cid']:'';//栏目id
		$aid = isset($attr['aid'])?$attr['aid']:'';//文章aid
		$row = isset($attr['row'])?$attr['row']:10;//显示条数
		$titlelen=isset($attr['titlelen'])?$attr['titlelen']:20;//标题长度
		
		$php=<<<php
		<?php
		\$cid='$cid';
		\$aid='$aid';
		\$db = M('content');
		if(\$cid)\$db->where("cid in(\$cid)");
		if(\$aid)\$db->where("aid in(\$aid)");
		\$data = \$db->where(\$where)->limit($row)->all();
		foreach(\$data as \$field):
			\$field['url']=U('Index/Index/content',array('aid'=>\$field['aid']));
			\$field['title']=mb_substr(\$field['title'],0,$titlelen,'utf-8');
		?>
php;
		$php.=$content;
		$php.='<?php endforeach;?>';
		return $php;
	}

//*****************************   _channel读取栏目    ***********************
	//_channel(标签属性,标签内容)
	public function _channel($attr,$content){
		$cid = $attr['cid'];
		$type=isset($attr['type'])?$attr['type']:'self';
		$php=<<<php
		<?php 
	\$cid=$cid;
	\$type=$type;
	\$db = M('category');
	switch(\$type){ 
		case 'self':
			\$cat = \$db->find(\$cid);//找到本身
			\$data = \$db->where("pid=".\$cat['pid'])->all();//找到兄弟栏目
			break;
		case 'son':
			\$data = \$db->where("pid=".\$cid)->all();//找到兄弟栏目
			break;
	}
	foreach(\$data as \$field):
		\$field['caturl']=U('Index/Index/category',array('cid'=>\$field['cid']));
	?>
php;
	$php.=$content;
	$php.='<?php endforeach;?>';
	return $php;
	}

//*****************************   _pagelist 分页列表数据    ***********************
// public function _pagelist($attr,$content){
// 	$row=isset($attr['row'])?$attr['row']:10;//每页显示条数
// 	$titlelen=isset($attr['titlelen'])?$attr['titlelen']:20;//标题长度
// 	$php=<<<php
// 	<?php
// 	$cid=$_GET['cid'];
// 	//取所有子栏目
// 	$cat=Date::channelList(M('category')->all(),$cid);
// 	//所有子栏目cid
// 	$cat=array_keys($cat);


	public function _pagelist($attr,$content){
		$row=isset($attr['row'])?$attr['row']:10;//每页显示条数
		$titlelen=isset($attr['titlelen'])?$attr['titlelen']:20;//标题长度
		$php=<<<php
<?php
	\$cid=\$_GET['cid']; //\$cid=Q('cid',0,'intval');
	//取所有子栏目
	\$cat = Data::channelList(M('category')->all(),\$cid);
	\$cat=array_keys(\$cat);//所有子栏目cid
	\$cat[]=\$cid;//当前栏目加入
	\$db= M('content');
	\$where['cid']=array('IN',\$cat);
	\$page = new Page(\$db->where(\$where)->count(),$row);//使用框架分页类
	\$data= \$db->where(\$where)->limit(\$page->limit())->all();//
	foreach(\$data as \$field):
		\$field['url']=U('Index/Index/content',array('aid'=>\$field['aid']));
		\$field['title']=mb_substr(\$field['title'],0,$titlelen,'utf-8');
		?>
php;
	$php.=$content;
	$php.="<?php endforeach;?>";
	return $php;
}

//*****************************   _pageshow 显示页码   ***********************
	public function _pageshow($attr,$content){
		return "<?php echo \$page->show();?>";
	}

}//***************标签类结束